$results = User::raw(function ($collection) use ($userId) {
return $collection->aggregate([
[
'$match' => [
'id' => [ '$in' => $id ]
]
],
[
'$group' => [
'_id' => '$user_id',
"total" => [ '$sum' => '$price' ]
]
],
]);
});
match 是匹配的条件
$gt -------- greater than >
$gte --------- gt equal >=
$lt -------- less than <
$lte --------- lt equal <=
$ne ----------- not equal !=
$eq -------- equal =
group 分组条件 total 统计总数($price 替换成1)或者总和($price 替换成指定字段)